package com.lzx.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.answer.entity.Report;
import com.lzx.answer.entity.vo.ReportParams;
import com.lzx.answer.mapper.ReportMapper;
import com.lzx.answer.service.ReportService;
import com.lzx.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户举报表 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2023-01-05
 */
@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {
    @Resource
    private ReportMapper reportMapper;

    //用户举报问题
    @Override
    public R reportProblem(Report report) {
        QueryWrapper<Report> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", report.getUserId()).eq("problem_id", report.getProblemId());
        Report vo = reportMapper.selectOne(wrapper);
        if (vo != null) {
            return R.ok();
        } else {
            report.setCreateTime(new Date());
            reportMapper.insert(report);
            return R.ok().message("举报成功");
        }
    }

    //根据用户id查询用户是否举报了当前这个问题
    @Override
    public R getUserIsReportProblem(int userId, int problemId) {
        QueryWrapper<Report> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("problem_id", problemId);
        Report report = reportMapper.selectOne(wrapper);
        if (report != null) {
            return R.ok().data("count", 1);
        } else {
            return R.ok().data("count", 0);
        }
    }

    //获取举报列表
    @Override
    public R getReportList(int current, int size) {
        Page<Report> page = new Page<>(current, size);
        reportMapper.selectPage(page, null);
        Map<String, Object> map = new HashMap<>();
        List<Report> list = page.getRecords();//当前页数据
        long total = page.getTotal();//总记录数
        map.put("list", list);
        map.put("total", total);
        return R.ok().data("map", map);
    }

    //审核举报
    @Override
    public R auditReport(Long id, String message) {
        Report report = reportMapper.selectById(id);
        report.setUpdateTime(new Date());
        report.setMessage(message);
        report.setIsDispose(1);
        reportMapper.updateById(report);
        return R.ok().message("审核成功");
    }

    //搜索
    @Override
    public R searchReport(ReportParams reportParams) {
        QueryWrapper<Report> wrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(reportParams.getContent())) {
            wrapper.like("content", reportParams.getContent());
        }
        if (reportParams.getIsDispose() != null) {
            wrapper.eq("is_dispose", reportParams.getIsDispose());
        }
        List<Report> list = reportMapper.selectList(wrapper);
        return R.ok().data("list", list);
    }
}
